//
//  WSTools.m
//  Travelling
//
//  Created by dllo on 15/11/17.
//  Copyright © 2015年 吴帅. All rights reserved.
//

#import "WSTools.h"

@implementation WSTools

+ (CGFloat)heightForContent:(NSString *)string size:(NSInteger) sizes{
    
    CGSize size = CGSizeMake(315 * ADAPT_WIDTH, 20000);
    NSDictionary *dic = [NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:sizes] forKey:NSFontAttributeName];

    CGRect frame = [string boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    return frame.size.height;
}

#pragma mark -- 清理
+ (void)clearCaches {
    
    NSString *cachPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    
    NSArray *files = [[NSFileManager defaultManager] subpathsAtPath:cachPath];
    
    for (NSString *p in files) {
        
        NSError *error;
        NSString *path = [cachPath stringByAppendingPathComponent:p];
        
        if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {
            
            [[NSFileManager defaultManager] removeItemAtPath:path error:&error];
        }
    }
    
}

#pragma mark -- 缓存文件大小
+ (float)folderSizeAtPath {
    
    NSFileManager* manager = [NSFileManager defaultManager];
    
     NSString *cachPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    
    if (![manager fileExistsAtPath:cachPath])
        return 0;
    
    NSEnumerator *childFilesEnumerator = [[manager subpathsAtPath:cachPath] objectEnumerator];
    
    NSString* fileName;
    
    long long folderSize = 0;
    
    while ((fileName = [childFilesEnumerator nextObject]) != nil){
        
        NSString* fileAbsolutePath = [cachPath stringByAppendingPathComponent:fileName];
        
        if ([manager fileExistsAtPath:fileAbsolutePath]){
            
            folderSize += [[manager attributesOfItemAtPath:fileAbsolutePath error:nil] fileSize];
        }
    }
    
    return folderSize/(1024.0*1024.0);
}

@end
